package org.example.struct.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/12/6 09:09
 * @apiNote
 */

public class Test00389FindTheDifference {
    public static void main(String[] args) {
        System.out.println(findTheDifference4("a", "aa"));
    }

    public static char findTheDifference5(String s, String t) {
       int ret = 0;
       for(int i = 0; i < s.length(); i++){
           ret ^= s.charAt(i);
       }
       for(int i = 0; i < t.length(); i++){
           ret ^= t.charAt(i);
       }
       return (char)ret;
    }

    public  static char findTheDifference4(String s, String t) {
        int as = 0,at = 0;
        for(int i = 0; i < s.length(); i++){
            as +=  s.charAt(i);
        }
        for(int i = 0; i < t.length(); i++){
            at += t.charAt(i);
        }
        return (char)(at -as);
    }

    public static char findTheDifference3(String s, String t) {
        char[] ss = s.toCharArray();
        Arrays.sort(ss);
        char[] tt = t.toCharArray();
        Arrays.sort(tt);
        for (int i = 0; i < tt.length; i++) {
            if (i == ss.length || ss[i] != tt[i]) {
                return tt[i];
            }
        }
        return ' ';
    }

    public static char findTheDifference2(String s, String t) {
        int[] arr = new int[26];
        for (char c : s.toCharArray()) {
            arr[c - 'a']++;
        }
        for (char c : t.toCharArray()) {
            arr[c - 'a']--;
        }
        for (int i = 0; i < 26; i++) {
            if (arr[i] != 0) {
                return (char) (i + 'a');
            }
        }
        return ' ';
    }

    public static char findTheDifference(String s, String t) {
        Map<Character, Integer> map = new HashMap<>();
        for (char c : s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for (char c : t.toCharArray()) {
            if (!map.containsKey(c) || map.get(c) == 0) {
                return c;
            } else {
                map.put(c, map.get(c) - 1);
            }
        }
        return ' ';
    }
}
